package com.xpsoft.app.user.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import sun.security.krb5.internal.crypto.Des3;

import net.sf.json.JSONObject;

import com.xpsoft.framework.context.ContextHolder;
import com.xpsoft.framework.service.Service;
import com.xpsoft.framework.util.DES;
import com.xpsoft.framework.util.HttpUtil;
import com.xpsoft.framework.web.adapter.struts2.BaseAction;
import com.xpsoft.app.user.UserLoginTypeConst;
import com.xpsoft.app.user.pojo.ClientInfo;
import com.xpsoft.app.user.pojo.PhoneAuthorization;
import com.xpsoft.app.user.pojo.User;

public class TransferAction extends BaseAction{
	private Service baseService = (Service) ContextHolder.getBean("baseService");

	public String  check() {
		String key = HttpUtil.getString(super.getRequest(), "key");
		JSONObject jsonBack = new JSONObject();
		jsonBack.put("success", false);
		Map map = new HashMap();
		if (StringUtils.isNotEmpty(key)) {
			DES des = new DES("key");//创建对象，赋予密钥
			String decryptResult = des.decrypt(key);//解密 
			try {
				String ip = decryptResult.split("&")[0];
				String imei = decryptResult.split("&")[1];
				//根据imei查询 PhoneAuthorization表 看是否已经授权
				map.put("deleteflag", 0);
				map.put("imei", imei);
				map.put("clientIp", ip); 
				PhoneAuthorization phoneAuthorization = null;
				List phoneAuthorizationList = baseService.getObjectsByParam(PhoneAuthorization.class, map, null, null);
				if (!phoneAuthorizationList.isEmpty()) {
					phoneAuthorization = (PhoneAuthorization) phoneAuthorizationList.get(0);
				}
				//如果已经授权
				if (phoneAuthorization != null) {
					//再根据ip查询 clientinfo 查询授权起始和截止时间
					map.clear();
					map.put("deleteflag", 0);
					map.put("ip", ip);
					ClientInfo clientInfo = null;
					List clientInfoList = baseService.getObjectsByParam(ClientInfo.class, map, null, null);
					if (!clientInfoList.isEmpty()) {
						clientInfo = (ClientInfo) clientInfoList.get(0);
					}
					Date now = new Date();
					//如果当前时间在授权时间内 {返回 true}
					if (clientInfo != null &&
							clientInfo.getBeginDate().before(now) && clientInfo.getEndDate().after(now)) {
						jsonBack.put("success", true);
					} else {
						jsonBack.put("success", false);
					}
				} else {
				//如果没有授权
					//根据ip查询 clientinfo 查询授权数目
					map.clear();
					map.put("deleteflag", 0);
					map.put("ip", ip);
					ClientInfo clientInfo = null;
					List clientInfoList = baseService.getObjectsByParam(ClientInfo.class, map, null, null);
					if (!clientInfoList.isEmpty()) {
						clientInfo = (ClientInfo) clientInfoList.get(0);
					}
					//如果授权数目剩余大于0 并且 当前系统时间在授权时间范围内
					if (clientInfo != null &&
							clientInfo.getUsedCount()<clientInfo.getCount()) {
						//插入该条授权 
						phoneAuthorization = new PhoneAuthorization();
						phoneAuthorization.setClientIp(ip);
						phoneAuthorization.setCreateDate(new Date());
						phoneAuthorization.setDeleteflag(0);
						phoneAuthorization.setImei(imei);
						baseService.save(phoneAuthorization);
						jsonBack.put("success", true);
					} else {
						//如果授权数目剩余小于0 或超过授权时间范围 授权失败 返回false}
						jsonBack.put("success", false);
					}
				}
			} catch (IndexOutOfBoundsException e) {
				// TODO: handle exception
				e.printStackTrace();
				jsonBack.put("success", false);
			} catch (Exception e) {
				e.printStackTrace();
				jsonBack.put("success", true);
			}		
		}
		PrintWriter pw = super.getPrintWriter();
		pw.append(jsonBack.toString());
		pw.flush();
		return null;
	}
}
